#include "uti_indexseries_mq.h"
#include "uti_interface.h"
#include "uti_consolecolor.h"

IndexSeries_Mq::IndexSeries_Mq(IndexSerInfo* pserinfo, HANDLE* phan):m_phan_callback(phan)
{
	if (!GetIndexSeries(pserinfo, &m_pser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[IndexSeries_Mq] GetIndexSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return;
	}


	m_pstart = (m_pser->m_pindexcur - m_pser->m_indexindex);

	m_index = 0;

	if (NULL != m_phan_callback)
		*m_phan_callback = CreateEvent(NULL, false, false, NULL);

	m_size = m_pser->m_indexserinfo.sersize;
};

IndexSeries_Mq::IndexSeries_Mq(unsigned int sersize, void (*p) (), HANDLE* phan):m_phan_callback(phan)
{
	if (!GetIndexSeries(sersize, p, &m_pser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[IndexSeries_Mq] GetIndexSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return;
	}


	m_pstart = (m_pser->m_pindexcur - m_pser->m_indexindex);

	m_index = 0;

	if (NULL != m_phan_callback)
		*m_phan_callback = CreateEvent(NULL, false, false, NULL);

	m_size = m_pser->m_indexserinfo.sersize;
}


IndexSeries_Mq::~IndexSeries_Mq()
{
	if (NULL != m_pser)
		delete m_pser;

	if (NULL != m_phan_callback)
		CloseHandle(*m_phan_callback);
};


int IndexSeries_Mq::Checkindex()
{
	int ret = 0;

	m_index++;

	if (m_index == m_size)
	{
		if (NULL != m_phan_callback)
			SetEvent(*m_phan_callback);
		

		m_index = 0;

		ret = 1;
	}

	return ret;

}